<?php
header("content-type:text/html;charset=utf-8");
session_start();
include '../public/config/config.php';
include "../public/include/funcs.php";

$code = $_GET['code'];
$state = $_GET['state'];

//获取access_token 和 openid
echo $code."<hr>";
echo $state."<hr>";
$path .= "https://api.weixin.qq.com/sns/oauth2/access_token";
$path .= "?appid=".$appid;
$path .= "&secret=".$appSecret;
$path .= "&code=".$code;
$path .= "&grant_type=authorization_code";
$json = file_get_contents($path);
echo $json."<hr>";
$json_arr = json_decode($json);
echo "<pre>";
print_r($json_arr);
echo "</pre>";
echo $json_arr->access_token."<hr>";
echo $json_arr->openid."<hr>";


//获取userinfo
$access_token = $json_arr->access_token;
$openid = $json_arr->openid;
$path2 .= "https://api.weixin.qq.com/sns/userinfo";
$path2 .= "?access_token=".$access_token;
$path2 .= "&openid=".$openid."&lang=zh_CN";
$json2 = file_get_contents($path2);
$json_arr2 = json_decode($json2);
echo "<pre>";
print_r($json_arr2);
echo "</pre>";
$nickname = $json_arr2->nickname;
$userImg = $json_arr2->headimgurl;
$sex = $json_arr2->sex;
var_dump($openid);
echo $nickname."<hr>";
echo $userImg."<hr>";


//查看该用户是否已经注册过
$link=conn($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd,$cfg_dbname,$cfg_dbchar);
$userArr = select($link, "user","openid='".$openid."'");
if($userArr){
	//已经注册过的用户，直接登录（把图片做一下更新）
	$arr['u_img'] = $json_arr2->headimgurl;
	$arr['nickname'] = $json_arr2->nickname;
	save($link, "user", $arr, "openid='".$openid."'");
	$_SESSION['name'] = $userArr[0]['tname'] ? $userArr[0]['tname'] : $json_arr2->nickname;
	$_SESSION['id'] = $userArr[0]['id'];
//	echo "<script>";
//	echo "alert('欢迎" .$name. "登录！');";
//	echo "window.location.href='user.php?openid=".$openid."';";
//	echo "</script>";
//	echo "<script>alert('欢迎" .$name. "登录！');</script>";
	header('location:user.php?id='.$_SESSION['id']);
}else{
	//没注册的用户，现在将信息写入到数据库
	if($openid != ""){
		$arr['pid'] = $state;
		$arr['pid2'] = $state;
		$arr['openid'] = $openid;
		$arr['nickname'] = $json_arr2->nickname;
		$arr['sex'] = $json_arr2->sex;
		$arr['country'] = $json_arr2->country;
		$arr['province'] = $json_arr2->province;
		$arr['city'] = $json_arr2->city;
		$arr['u_img'] = $json_arr2->headimgurl;
		$arr['int_time'] = time();
		$re = add($link, "user", $arr);
		$_SESSION['name'] = $json_arr2->nickname;
		$newUserArr = select($link, "user","openid='".$openid."'");
		$_SESSION['id'] = $newUserArr[0]['id'];
		if($re){
			$u_id = $newUserArr[0]['id'];
			
			$hid_arr['u_id'] = $u_id;
			$hid_arr['row1'] = 1;
			$hid_arr['row2'] = 1;
			$hid_arr['row3'] = 1;
			$hid_arr['row4'] = 1;
			$hid_arr['row5'] = 1;
			$hid_arr['row6'] = 0;
			$hid_arr['row7'] = 0;
			$hid_arr['row8'] = 0;
			$hid_arr['row9'] = 0;
			$hid_arr['row10'] = 0;
			$hid_arr['row11'] = 1;
			
			$re2 = add($link, 'hidden', $hid_arr);
			
//			echo "<script>alert('注册成功！');</script>";
			header('location:user.php?id='.$_SESSION['id']);
		}else{
			echo "<script>alert('注册失败！');</script>";
		}
	}else{
		echo "<script>alert('用户openID获取失败！');</script>";
	}
}








?>